package com.zhxh.nzpos.api;

import org.ksoap2.serialization.SoapObject;

import com.zhxh.nzpos.api.exception.AuthenticationException;
import com.zhxh.nzpos.api.exception.AuthorizationException;
import com.zhxh.nzpos.api.exception.CommunicationException;
import com.zhxh.nzpos.model.Category;

public class ConnectorCategory {

	private KSoapExecutor soap;

	public ConnectorCategory() {

		soap = new KSoapExecutor();
	}

	public Category[] getCategorys() throws CommunicationException,
			AuthorizationException, AuthenticationException {

		SoapObject getCategorys = SoapObjectBuilder.start()
				.withMethod("GoodsCategoryList")
				.withProperty("Key", SoapObjectBuilder.KEY).build();

		// @SuppressWarnings("unchecked")
		SoapObject vc = null;
		try {
			vc = (SoapObject) soap.execute(getCategorys);
			System.out.println("#####################vc=" + vc);
		} catch (org.apache.http.auth.AuthenticationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if (vc == null)
			return null;

		int count = vc.getPropertyCount();
		Category[] categorys = new Category[count];

		SoapObject p;
		for (int i = 0; i < count; ++i) {
			p = (SoapObject) vc.getProperty(i);

			// Creating new goods with all fields
			Category category = new Category();
			category.setId(p.getPropertySafelyAsString("ID"));
			category.setCategory(p.getPropertySafelyAsString("Category"));

			categorys[i] = category;

		}
		return categorys;
	}

}
